import Mock from 'mockjs'
import { questionListItemType } from '@/types'
import getStatList from './data/getStatList'
import getComponentList from './data/getComponentList'

const list = Mock.mock({
  'clist|50': [
    {
      _id: '@id',
      title: '@ctitle(10,20)',
      isStar: '@boolean',
      isPublished: '@boolean',
      answerCount: '@integer(1, 100)',
      creatAt: '@datetime',
      isDelete: '@boolean',
    },
  ],
}).clist

const rules = [
  {
    // 获取单个问卷信息
    url: '/api/question/:id',
    method: 'get',
    response: () => {
      return {
        errno: 0,
        data: {
          id: '@id',
          title: '@ctitle(1, 5)',
          desc: '@ctitle(1, 10)',
          js: '',
          css: '',
          isPublished: true,
          componentList: getComponentList(),
        },
      }
    },
  },
  {
    // 创建问卷
    url: '/api/question',
    method: 'post',
    response: () => {
      return {
        errno: 0,
        data: {
          id: '@id',
        },
      }
    },
  },
  {
    // 获取问卷列表
    url: '/api/question',
    method: 'get',
    response: (options: any) => {
      const { keyword, type = 0, page = 1, pageSize = 5 } = options.query as any
      let clist = list

      if (keyword && keyword.trim()) {
        clist = clist.filter((item: questionListItemType) => item.title.includes(keyword))
      }

      switch (type) {
        case '0': // 我的问卷
          clist = clist.filter((item: questionListItemType) => !item.isDelete)
          break
        case '1': // 星标问卷
          clist = clist.filter((item: questionListItemType) => item.isStar && !item.isDelete)
          break
        case '2': // 回收站
          clist = clist.filter((item: questionListItemType) => item.isDelete)
          break
        default:
          break
      }

      const start = (page - 1) * pageSize
      const end = page * pageSize
      const pageList = clist.slice(start, end)

      return {
        errno: 0,
        data: {
          list: pageList,
          total: clist.length,
        },
      }
    },
  },

  {
    // 更新问卷
    url: '/api/question/:id',
    method: 'patch',
    response: () => {
      return {
        errno: 0,
      }
    },
  },
  {
    // 复制问卷
    url: '/api/question/duplicate/:id',
    method: 'post',
    response: () => {
      return {
        errno: 0,
        data: {
          id: '@id',
        },
      }
    },
  },
  {
    // 批量彻底删除
    url: '/api/question',
    method: 'delete',
    response: () => {
      return {
        errno: 0,
      }
    },
  },
  {
    // 获取用户信息
    url: '/api/user/info',
    method: 'get',
    response: () => {
      return {
        errno: 0,
        // msg: '获取用户信息失败',
        data: {
          id: '@id',
          username: '@title',
          nickname: '@cname',
        },
      }
    },
  },
  {
    // 登录
    url: '/api/user/login',
    method: 'post',
    response: () => {
      return {
        errno: 0,
        data: {
          token: '@guid',
        },
      }
    },
  },
  {
    // 注册
    url: '/api/user/register',
    method: 'post',
    response: () => {
      return {
        errno: 0,
      }
    },
  },
  {
    // 答卷列表
    url: '/api/state/:id',
    method: 'get',
    response: (res: any) => {
      const { page = 1, pageSize = 5 } = res.query
      return {
        errno: 0,
        data: {
          total: 100,
          list: getStatList(page, pageSize),
        },
      }
    },
  },
  {
    // 获取单个组件的统计数据汇总
    url: '/api/state/:questionId/:componentId',
    method: 'get',
    response: () => {
      return {
        errno: 0,
        data: {
          state: [
            { name: '选项1', count: 10 },
            { name: '选项2', count: 20 },
            { name: '选项3', count: 30 },
          ],
        },
      }
    },
  },
]

export default rules
